import en from './en';
import zh from './zh';

// Available languages
const languages = {
  en,
  zh
};

// Default language
const defaultLanguage = 'en';

// Get current language from storage or use default
const getCurrentLanguage = () => {
  try {
    const storedLanguage = wx.getStorageSync('userLanguage');
    return storedLanguage && languages[storedLanguage] ? storedLanguage : defaultLanguage;
  } catch (e) {
    console.error('Error getting language from storage:', e);
    return defaultLanguage;
  }
};

// Initialize language
let currentLanguage = getCurrentLanguage();

// Get translations for current language
const t = (key) => {
  const translations = languages[currentLanguage];
  return translations[key] || languages[defaultLanguage][key] || key;
};

// Change language
const setLanguage = (lang) => {
  if (languages[lang]) {
    currentLanguage = lang;
    wx.setStorageSync('userLanguage', lang);
    return true;
  }
  return false;
};

// Get current language code
const getLanguage = () => currentLanguage;

// Check if current language is RTL (for future support)
const isRTL = () => {
  const rtlLanguages = ['ar', 'he', 'fa', 'ur'];
  return rtlLanguages.includes(currentLanguage);
};

export default {
  t,
  setLanguage,
  getLanguage,
  isRTL,
  languages: Object.keys(languages)
};